package com.ljph.ljaker.service;

import com.jfinal.plugin.activerecord.Db;
import com.ljph.jfinal.base.BaseService;
import com.ljph.kit.CommonKit;
import com.ljph.ljaker.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yuzhou on 16/9/18.
 */
public class UserService extends BaseService {

    private static final Logger _log = LoggerFactory.getLogger(UserService.class);

    public boolean save(User user) {
        return user.save();
    }

    public boolean update(final User user, final HashMap<String, Object> changed) {

        final List<Object> params = new ArrayList<Object>();

        final StringBuilder sql = new StringBuilder();
        sql.append("update " + CommonKit.getTableName(User.class) + " set updateTime = ?");
        params.add(new Date());
        changed.keySet().forEach((key) -> {
            sql.append(", " + key + " = ?");
            params.add(changed.get(key));
        });

        sql.append(" where id = ? ");
        params.add(user.getId());

        return Db.update(sql.toString(), params.toArray()) == 1;
    }
}
